package cn.tedu.egweibo1.controller;


import cn.tedu.egweibo1.mapper.WeiboMapper;
import cn.tedu.egweibo1.pojo.dto.WeiboDTO;
import cn.tedu.egweibo1.pojo.entity.Weibo;
import cn.tedu.egweibo1.pojo.vo.ContentListVO;
import cn.tedu.egweibo1.pojo.vo.UserVO;
import cn.tedu.egweibo1.pojo.vo.WeiboDetailVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("/v1/weibo/")
public class WeiboController {
    @Autowired(required = false)
    private WeiboMapper weiboMapper;

    /**
     * 发布微博功能
     * 接口地址: /v1/weibo/insert
     * 请求方式: POST
     * 请求数据类型: application/json
     * 响应数据类型: int
     */
    @RequestMapping("insert")
    public int insert(@RequestBody WeiboDTO weiboDTO, HttpSession session){
        UserVO userVO = (UserVO) session.getAttribute("user");
        if(userVO==null){
            return 2;
        }
        Weibo weibo = new Weibo();
        BeanUtils.copyProperties(weiboDTO,weibo);
        weibo.setUserId(userVO.getId());
        weiboMapper.insertWeiboByUid(weibo);
        return 1;
    }

    /**
     * 首页微博列表功能
     * 接口地址: /v1/weibo/selectIndex
     * 请求方式: GET
     * 请求数据类型: application/x-www-form-urlencoded
     * 响应数据类型: 见响应参数
     */
    @RequestMapping("selectIndex")
    public List<ContentListVO> selectIndex(){
        List<ContentListVO> list = weiboMapper.selectWeiboList();
        return list;
    }

    /**
     * 微博详情功能
     * 接口地址: /v1/weibo/selectById
     * 请求方式: GET
     * 请求数据类型: application/x-www-form-urlencoded
     * 响应数据类型: 见响应参数
     */
    @RequestMapping("selectById/{id}")
    public WeiboDetailVO selectById(@PathVariable int id){
        WeiboDetailVO weiboDetailVO = weiboMapper.selectWeiboById(id);
        return weiboDetailVO;
    }
}
